package deepr.filter;

import deepr.lib.FilterLib;
import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
// select id from asset where asset_category_id =
//  any(select id from asset_category ac where location_id =
//      any(select id from location where id = 1))
public class FilterRelay {
    // 父对象
    private String parent;
    private HashMap<String, StringBuilder> propertyFilter = new HashMap<>();
    private HashMap<String, StringBuilder> jsonArrayFilter = new HashMap<>();

    public FilterRelay(String parent) {
        this.parent = parent;
    }

    //    public StringBuilder concat(String joiner, String object) {
//        StringBuilder sb1 = concatPropertyFilter(joiner);
//        StringBuilder sb2 = concatJsonArrayFilter(joiner);
//        StringBuilder sb = new StringBuilder();
//        if (sb1.length() > 0 && sb2.length() > 0) {
//            sb1.append(joiner).append(sb2);
//        } else if (sb2.length() > 0)
//            sb1.append(sb2);
//        if (parent == null)
//            return sb1;
//        SchemaData parentSchema = SchemaManager.getSchemaData(parent);
//        PropertyData me = parentSchema.getFields().get(object);
//        String forProperty = me.getForProperty();
//        SchemaData meSchema = SchemaManager.getSchemaData(object);
//        String myName = meSchema.getName();
//        String primaryKey = meSchema.getPrimaryKey();
//        sb.append(forProperty).append("=ANY(SELECT ").append(primaryKey).append(" FROM ").append(myName).append(" WHERE ").append(sb1).append(")");
//        return sb;
//    }
//
//    public StringBuilder concatJsonArrayFilter(String joiner) {
//        if (jsonArrayFilter.size() == 0)
//            return new StringBuilder();
//        StringBuilder from = new StringBuilder();
//        StringBuilder sb2 = new StringBuilder();
//        for (Map.Entry<String, StringBuilder> foreignEntry : jsonArrayFilter.entrySet()) {
//            String fJK = foreignEntry.getKey();
//            FilterLib.appendJoiner(from, ",");
//            from.append("JSONB_ARRAY_ELEMENTS(").append(fJK).append(") AS ").append(fJK).append("_ele");
//            StringBuilder fJKSb = foreignEntry.getValue();
//            FilterLib.appendJoiner(sb2, joiner);
//            sb2.append(fJKSb);
//        }
//        return new StringBuilder().append("EXISTS(SELECT 1 FROM ").append(from).
//                append(" WHERE ").append(sb2).append(" LIMIT 1)");
//    }
//
//
    public StringBuilder getProperty(String joiner) {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, StringBuilder> entry : propertyFilter.entrySet()) {
            if (entry.getValue().length() > 0) {
                FilterLib.appendJoiner(sb, joiner);
                sb.append(entry.getValue());
            }
        }
        return sb;
    }

    public void appendProperty(String property, StringBuilder newSb, String joiner) {
        StringBuilder sb = propertyFilter.getOrDefault(property, new StringBuilder());
        if (newSb != null) {
            FilterLib.appendJoiner(sb, joiner);
            sb.append(newSb);
        }
        propertyFilter.put(property, sb);
    }

    public void appendJsonArray(String property, StringBuilder newSb, String joiner) {
        StringBuilder sb = jsonArrayFilter.getOrDefault(property, new StringBuilder());
        if (newSb != null) {
            FilterLib.appendJoiner(sb, joiner);
            sb.append(newSb);
        }
        jsonArrayFilter.put(property, sb);
    }


}
